package com.i2works.smartluolong.utils.plugin;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.utils.base.GlobalConstant;

import java.io.Serializable;
import java.util.List;

/**
 * 分页
 *
 * @author Liu Yutao {koal@vip.qq.com}
 * @date 2016/10/21 12:32
 */

public class Pager implements Serializable {
    private Integer total;                                              //信息总条数
    private Integer totalPage;                                          //总页数
    private Integer pageNum = 0;                                        //当前页码
    private Integer start=1;                                              //开始
    private Integer offset;                                             //结束
    private Integer pre = 0;                                            //上一页ID
    private Integer next = 0;                                           //下一页ID
    private Integer SIZE = GlobalConstant.page.PAGE_SIZE;   //分页个数
    private List<Integer> pages = Lists.newArrayList();

    public Pager(Integer pageNum, Integer total,Integer SIZE) {
        this.pageNum = pageNum;
        this.total = total;
        this.SIZE=SIZE;
        getTotalPage();
        getStart();
        getOffset();
        getPages();
        getPre();
        getNext();
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getTotalPage() {
        if (total % SIZE == 0) {
            totalPage = total / SIZE;
        } else {
            totalPage = total / SIZE + 1;
        }
        return totalPage;
    }

    public static Integer getTotalPage(Integer total, Integer size) {
        Integer totalPage = 0;
        if (total % size == 0) {
            totalPage = total / size;
        } else {
            totalPage = total / size + 1;
        }
        return totalPage;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getStart() {
        return start;
    }

    public Integer getOffset() {
    	offset=totalPage;
        return offset;
    }


    public List<Integer> getPages() {
        pages.clear();
        Integer s = (pageNum - offset) > 0 ? (pageNum - offset) : 1;
        Integer e = (pageNum + offset) > totalPage ? totalPage : (pageNum + offset);
        for (Integer i = s; i <= e; i++) {
            pages.add(i);
        }
        return pages;
    }

    public void setPages(List<Integer> pages) {
        this.pages = pages;
    }

    public Integer getPre() {
        pre = pageNum - 1;
        if (pre == 0) {
            return pageNum;
        } else {
            return pre;
        }
    }

    public void setPre(Integer pre) {
        this.pre = pre;
    }

    public Integer getNext() {
        next = pageNum + 1;
        if(pageNum == totalPage ){
            return totalPage;
        }else {
            return next;
        }
    }

    public void setNext(Integer next) {
        this.next = next;
    }
}
